package com.huaruan.youdi.views.activitys;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.huaruan.youdi.R;
import com.huaruan.youdi.adapter.HistoryAdapter;
import com.huaruan.youdi.http.okhttp.Constant;
import com.huaruan.youdi.http.okhttp.HttpCallback;
import com.huaruan.youdi.http.okhttp.HttpPageUtils;
import com.huaruan.youdi.models.HistoryModel;
import com.huaruan.youdi.utils.AppUtils;
import com.huaruan.youdi.utils.DeviceUtils;
import com.huaruan.youdi.utils.TimeUtils;
import com.huaruan.youdi.utils.UserUtils;
import com.huaruan.youdi.views.dialogs.DatePickerDialog;
import com.huaruan.youdi.views.widget.ActionListener;
import com.huaruan.youdi.views.widget.easyrecyclerview.EasyRecyclerView;
import com.huaruan.youdi.views.widget.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.huaruan.youdi.views.widget.easyrecyclerview.decoration.SpaceDecoration;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;

public class HistoryActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;

    View head;
    TextView tv_date;
    TextView tv_count;
    TextView tv_price;
    HistoryAdapter historyAdapter;
    Calendar calendar;

    String years;
    String month;

    HttpPageUtils httpPageUtils;

    @Override
    public int IGetMultiSatesContentViewResId() {
        return R.layout.activity_history;
    }

    @Override
    public int IGetContentViewResId() {
        return 0;
    }

    @Override
    public void IFindViews() {
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(IGetContext(),
                LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        SpaceDecoration itemDecoration = new SpaceDecoration(DeviceUtils.dp2px(5));
        recyclerView.addItemDecoration(itemDecoration);
        historyAdapter = new HistoryAdapter(IGetActivity());
        recyclerView.setAdapter(historyAdapter);

        head = View.inflate(IGetContext(), R.layout.activity_history_head, null);
        tv_date = (TextView) head.findViewById(R.id.tv_date);
        tv_count = (TextView) head.findViewById(R.id.tv_count);
        tv_price = (TextView) head.findViewById(R.id.tv_price);
        historyAdapter.addHeader(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {
                return head;
            }

            @Override
            public void onBindView(View headerView) {

            }
        });

        historyAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
            }
        });

        historyAdapter.setMore(R.layout.loadmore, new RecyclerArrayAdapter.OnMoreListener() {
            @Override
            public void onMoreShow() {
                httpPageUtils.next();
            }

            @Override
            public void onMoreClick() {

            }
        });

        tv_date.setText(TimeUtils.formatPhotoMonth(TimeUtils.getCurrentTime()));
        calendar = Calendar.getInstance();
        tv_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog = new DatePickerDialog();
                datePickerDialog.setActionListener(new ActionListener() {
                    @Override
                    public void onDoneClick(String one, String two) {
                        tv_date.setText(one + "年" + two + "月");
                        years = one;
                        month = two;
                        getOrderHistory();
                    }
                });
                datePickerDialog.show(getSupportFragmentManager(), "");
            }
        });
    }

    @Override
    public void IInitData() {

    }

    @Override
    public void IRequest() {
        String[] arr = TimeUtils.formatPhotoDate(TimeUtils.getCurrentTime()).split("-");
        if (arr.length > 1) {
            years = arr[0];
            month = arr[1];
            getOrderHistory();
        }
    }

    private void getOrderHistory() {
        SortedMap<String, String> sortMap = new TreeMap<>();
        sortMap.put("riderId", UserUtils.getUid());
        sortMap.put("years", years);
        sortMap.put("month", month);
        httpPageUtils = new HttpPageUtils(IGetContext())
                .url(Constant.GET_HISTORY)
                .params(sortMap)
                .modelClass(HistoryModel.class)
                .pageSize(10)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object o) {
                        hideLoadingAndRetryView();
                        if (httpPageUtils.isFirstPage()) {
                            historyAdapter.clear();
                        }
                        ArrayList<HistoryModel> models = (ArrayList<HistoryModel>) o;
                        if (httpPageUtils.isFirstPage() && models.size() == 0) {
                            tv_count.setText("0");
                            tv_price.setText("0");
                            AppUtils.toastShort(IGetContext(), "暂无接单");
                            return;
                        }else{
                            historyAdapter.addAll(models);
                        }
                        if (httpPageUtils.isFirstPage() && models.size() > 0) {
                            tv_count.setText(models.get(0).total);
                            tv_price.setText(models.get(0).totalAmount);
                        }
                    }

                    @Override
                    public void failure(int status, String errorMsg) {
                        super.failure(status, errorMsg);
                        hideLoadingAndRetryView();
                    }
                });
        httpPageUtils.first();
    }

    @Override
    public boolean isShowBackButtonWhite() {
        return true;
    }

    @Override
    public boolean HideStatusBar() {
        return true;
    }
}
